package com.jiangcheng.utils.database;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtil {

	private static Properties map = new Properties();
	private static final ThreadLocal<Connection> tol = new ThreadLocal<Connection>();
	
	static{
		InputStream is = null;
		
		try {
			//is = JdbcUtil.class.getResourceAsStream("/conf/db_server.properties");
            is = JdbcUtil.class.getResourceAsStream("/db_server.properties");
			map.load(is);
		} catch (Exception e) {
			throw new RuntimeException("读取配置错误");
		} finally {
			if(is != null) try{ is.close(); }catch(Exception e) {}
		}
	}

    /**
     * 获取连接
     * @return
     */
	public static Connection getConnection(){
		try {
			Connection con = tol.get();
			if(con == null){
				Class.forName(map.getProperty("jdbc.driver"));
				con = DriverManager.getConnection(map.getProperty("jdbc.url"), map.getProperty("jdbc.username"), map.getProperty("jdbc.password"));
				tol.set(con);
			}
			return con;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("获取连接错误");
		}
	}

    /**
     * 处理返回结果集
     */
    private static void printResultSet(ResultSet rs) {
        try {
            ResultSetMetaData meta = rs.getMetaData();
            int cols = meta.getColumnCount();
            StringBuffer b = new StringBuffer();
            while (rs.next()) {
                for (int i = 1; i <= cols; i++) {
                    b.append(meta.getColumnName(i) + "=");
                    b.append(rs.getString(i) + "\t");
                }
                b.append("\n");
            }
            System.out.print(b.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     * @param rs
     * @param stmt
     * @param con
     */
	public static void closeAll(ResultSet rs,Statement stmt,Connection con){
		if(rs != null) try { rs.close(); } catch (Exception e) {}
		if(stmt != null) try { stmt.close(); } catch (Exception e) {}
		if(con != null) try { con.close(); } catch (Exception e) {}
	}

	/**
	 * 带事物回退功能的批量删除处理
	 * @param sql
	 * @param param
	 * @return
	 */
	public boolean updateBatchDel(String sql,String[] param){
		boolean flag = false;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs =null;
		try {
			con = getConnection();
			con.setAutoCommit(false);
			pstmt = con.prepareStatement(sql);
			for (int i = 0; i < param.length; i++) {
				pstmt.setString(i, param[i].trim());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			con.commit();
			flag = true;
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
		} finally {
			closeAll(null,pstmt,con);
		}
		return flag;
	}

}
